/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.api.dto.AddButtonReq;
import org.cshoo.tattoo.authorize.api.dto.UpdateButtonReq;
import org.cshoo.tattoo.authorize.app.ButtonAppService;
import org.cshoo.tattoo.authorize.domain.po.Button;
import org.cshoo.tattoo.embed.api.dto.ValidResultResp;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Api(tags = "前端按钮级权限点接口")
@RestController
@RequestMapping(value = "/button")
@Slf4j
public class ButtonController {

    @Resource
    private ButtonAppService buttonAppService;

    @RequestMapping(value = "/check/code", method = RequestMethod.GET)
    @ApiOperation("校验按钮编号是否存在")
    public ValidResultResp checkButtonCodeAlreadyExists(String code, Long menuId) {
        log.info("code:{}, appId:{}", code, menuId);
        ValidResultResp result = new ValidResultResp();
        boolean exists = buttonAppService.buttonCodeExists(code, menuId);
        result.setValid(!exists);
        return result;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加按钮")
    public Response<?> addButton(@RequestBody AddButtonReq addButtonReq) {
        buttonAppService.addButton(addButtonReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation("修改按钮")
    public Response<?> updateButton(@RequestBody UpdateButtonReq updateButtonReq) {
        buttonAppService.updateButton(updateButtonReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiOperation("删除按钮")
    public Response<?> deleteButton(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        buttonAppService.deleteButton(id);
        return Response.builder().build();
    }

    @RequestMapping(value = "/findButtonsByMenu/{menuId}", method = RequestMethod.GET)
    @ApiOperation("查询某个菜单下的按钮")
    public List<Button> findButtonsByMenu(@PathVariable("menuId") Long menuId) {
        log.info("menuId:{}", menuId);
        return buttonAppService.findButtonsByMenu(menuId);
    }

    @RequestMapping(value = "/findByRoleId/{roleId}", method = RequestMethod.GET)
    @ApiOperation("查询某个角色有权限的按钮")
    public List<Button> findByRoleId(@PathVariable("roleId") Long roleId) {
        log.info("roleId:{}", roleId);
        return buttonAppService.findByRoleId(roleId);
    }

}
